package com.bjpowernode.dataservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bjpowernode.api.service.PlatformInfoService;
import com.bjpowernode.api.vo.PlatformInfo;
import com.bjpowernode.contacts.RedisKey;
import com.bjpowernode.dataservice.mapper.BidInfoMapper;
import com.bjpowernode.dataservice.mapper.ProductInfoMapper;
import com.bjpowernode.dataservice.mapper.SysUserMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: panDeng_Zhu
 * @time: 2022/10/24 10:49
 */
@DubboService(interfaceClass = PlatformInfoService.class, version = "1.0")
public class PlatformInfoServiceImpl implements PlatformInfoService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private BidInfoMapper bidInfoMapper;

    @Resource
    private ProductInfoMapper productInfoMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    //平台信息接口
/*    @Override
    public PlatformInfo queryIndexPlatInfo() {
        //查询数据库
        //1.查询注册用户数
        long registerAllUsers = sysUserMapper.selectCountId();
        //2.累计成交金额
        BigDecimal BidAllMoney = bidInfoMapper.selectSumBidMoney();
        //3.年平均利率
        BigDecimal historyAvgRate = productInfoMapper.selectAvgRate();
        PlatformInfo info = new PlatformInfo(historyAvgRate, registerAllUsers, BidAllMoney);
        return info;
    }*/

    @Override
    public PlatformInfo queryIndexPlatInfo() {
        PlatformInfo info = null;
        ValueOperations<String,String> vo = stringRedisTemplate.opsForValue();
        //1.从redis中获取
        String json = vo.get(RedisKey.PLATFORM_INFO);
        if (StringUtils.isNotBlank(json)){
            info = JSONObject.parseObject(json,PlatformInfo.class);
        }else{
            synchronized (this){
                //重新获取数据，这个数据可能是之前的方法调用储存的redis数据
                json = vo.get(RedisKey.PLATFORM_INFO);
                if (StringUtils.isBlank(json)){
                    //查询数据库
                    //1.查询注册用户数
                    long registerAllUsers = sysUserMapper.selectCountId();
                    //2.累计成交金额
                    BigDecimal BidAllMoney = bidInfoMapper.selectSumBidMoney();
                    //3.年平均利率
                    BigDecimal historyAvgRate = productInfoMapper.selectAvgRate();
                    info = new PlatformInfo(historyAvgRate, registerAllUsers, BidAllMoney);
                    //把info转为json
                    json=JSONObject.toJSONString(info);
                    //写入到redis
                    vo.set(RedisKey.PLATFORM_INFO,json,30, TimeUnit.MINUTES);
                }else{
                    info=JSONObject.parseObject(json,PlatformInfo.class);
                }
            }
        }
        return info;
    }
}
